from django.core.management.base import BaseCommand

from apps.app_service.models import OrgDataCenter


class Command(BaseCommand):
    help = """
    manage.py datacenter_rename"
    """

    def handle(self, *args, **options):
        count = 0
        dc_qs = OrgDataCenter.objects.all()
        self.stdout.write(self.style.SUCCESS(f"DC Count: {len(dc_qs)}"))
        for dc in dc_qs:
            old_name = dc.name
            dc.name = old_name.replace('数据中心', '云节点')
            if dc.name != old_name:
                dc.save(update_fields=['name'])
                self.stdout.write(self.style.SUCCESS(f"rename: “{old_name}” => “{dc.name}”"))
                count += 1

        self.stdout.write(self.style.SUCCESS(f"Rename count: {count}"))
